package pm.gui;

import org.apache.log4j.Logger;
import pm.config.Configuration;
import pm.gui.components.basepanels.MainPanel;
import pm.gui.components.basepanels.PrintFileChoosePanel;
import pm.gui.components.basepanels.PrintPanel;
import pm.gui.components.basepanels.SplashPanel;
import pm.gui.frames.AdminFrame;
import pm.gui.frames.PMFrame;

import java.awt.*;
import java.awt.event.*;
import pm.PM;

/**
 * Common implementation of <code>GuiModule</code> that contains
 * platform independent code.
 *
 * @author Anton Bernatski
 */
public class GuiModuleImpl extends AbstractGuiModule {

	private final static Logger logger = Logger.getLogger(GuiModule.class);

	public GuiModuleImpl(final GuiModuleListener guiModuleListener, final Configuration configuration) {
		this.guiModuleListener = guiModuleListener;
		this.configuration = configuration;
		init();
	}

	private void init() {
		pmFrameInit();
//		adminFrameInit();
		this.splashPanel = new SplashPanel();
		mainPanelInit();
		printPanelInit();
//		printFileChooseInit();
	}

	private void printFileChooseInit() {
		this.printFileChoosePanel = new PrintFileChoosePanel();
		this.printFileChoosePanel.driveComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				driveSelected();
			}
		});
		this.printFileChoosePanel.backButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				backButton(PanelType.PrintFileChoose);
			}
		});
		this.printFileChoosePanel.openButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				openFile();
			}
		});
	}

	private void printPanelInit() {
		this.printPanel = new PrintPanel();
		this.printPanel.printButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				printPanelPrintButton();
			}
		});
		this.printPanel.nextButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				documentViewPanelNextButton();
			}
		});
		this.printPanel.previousButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				documentViewPanelPreviousButton();
			}
		});
		this.printPanel.backButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				backButton(PanelType.Print);
			}
		});
	}

	private void mainPanelInit() {
		this.mainPanel = new MainPanel();
		this.mainPanel.printButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mainPanelPrintButton();
			}
		});
		this.mainPanel.copyButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mainPanelCopyButton();
			}
		});
		this.mainPanel.scanButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mainPanelScanButton();
			}
		});
	}

	private void adminFrameInit() {
		this.adminFrame = new AdminFrame();
		this.pmFrame.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(final KeyEvent e) {
				if (configuration.getAdminKey().charAt(0) == e.getKeyChar()) {
					adminFrame.setVisible(true);
				}
			}
		});
	}

	private void pmFrameInit() {
		final GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
		final GraphicsDevice[] devices = graphicsEnvironment.getScreenDevices();

		if (logger.isDebugEnabled()) {
			logger.debug("Graphics devices:");
			for (final GraphicsDevice device : devices) {
				device.getConfigurations();
				logger.debug("Device id: " + device.getIDstring());
				logger.debug("Device type: " + device.getType());
			}
		}

        if (PM.DEBUG && devices.length > 1) {
            this.pmFrame = new PMFrame(devices[1].getDefaultConfiguration());
        } else {
            this.pmFrame = new PMFrame(devices[0].getDefaultConfiguration());
        }
		this.pmFrame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				guiModuleListener.exit();
			}
		});
	}


}
